/*
 ============================================================================
 Name        : log4cpp.cpp
 Author      : raoqiang
 Version     :
 Copyright   : GPL
 Description : logger for cpp,
 ============================================================================
 */
#include "log4cpp.h"

/*
 *日志前缀
 *
 */
char* LOGGER_PREFIX[] = {"[DEBUG] ", "[INFO] ", "[WARN] ", "[ERROR] ", "[FATAL] "};

Logger::Logger()
{
}

Logger::~Logger()
{
}

bool Logger::init()
{
	return true;
}

void Logger::log(Level level, const char* msg)
{
	assert(level >= DEBUG && level <= FATAL);
	char buffer[MAX_LINE_LENGTH];
	memset(buffer, 0x00, MAX_LINE_LENGTH);

	int prefixsize = strlen(LOGGER_PREFIX[level]);
	strcpy(buffer, LOGGER_PREFIX[level]);
	strcpy(buffer + prefixsize, msg);

	// 代理给具体实现
	log(buffer);
}

void Logger::logv(Level level, const char* format, ...)
{
	char buffer[MAX_LINE_LENGTH];
	memset(buffer, 0x00, MAX_LINE_LENGTH);

	va_list args;
	va_start(args, format);
	int size = snprintf(buffer, MAX_LINE_LENGTH, format, args);
	va_end(args);

	// 代理给具体实现
	log(level, buffer);
}

void Logger::log_debug(const char* msg)
{
	log(DEBUG, msg);
}

void Logger::log_debug_v(const char* format, ...)
{
	char buffer[MAX_LINE_LENGTH];
	memset(buffer, 0x00, MAX_LINE_LENGTH);

	va_list args;
	va_start(args, format);
	int size = snprintf(buffer, MAX_LINE_LENGTH, format, args);
	va_end(args);

	log_debug(buffer);
}

void Logger::log_info(const char* msg)
{
	log(INFO, msg);
}

void Logger::log_info_v(const char* format, ...)
{
	char buffer[MAX_LINE_LENGTH];
	memset(buffer, 0x00, MAX_LINE_LENGTH);

	va_list args;
	va_start(args, format);
	int size = snprintf(buffer, MAX_LINE_LENGTH, format, args);
	va_end(args);

	log_info(buffer);
}

void Logger::log_warn(const char* msg)
{
	log(WARN, msg);
}

void Logger::log_warn_v(const char* format, ...)
{
	char buffer[MAX_LINE_LENGTH];
	memset(buffer, 0x00, MAX_LINE_LENGTH);

	va_list args;
	va_start(args, format);
	int size = snprintf(buffer, MAX_LINE_LENGTH, format, args);
	va_end(args);

	log_warn(buffer);
}

void Logger::log_fatal(const char* msg)
{
	log(FATAL, msg);
}

void Logger::log_fatal_v(const char* format, ...)
{
	char buffer[MAX_LINE_LENGTH];
	memset(buffer, 0x00, MAX_LINE_LENGTH);

	va_list args;
	va_start(args, format);
	int size = snprintf(buffer, MAX_LINE_LENGTH, format, args);
	va_end(args);

	log_fatal(buffer);
}
